<template>
  <div>
    <!-- 搜索 -->
    <!-- 技术报告查询 - 维修工单选择 -->
    <TopFilter :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="90" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      height="auto"
      selectionType="single"
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :fetchapi="fetchApi"
      :datakey="'rows'"
      :onColumnsChange="(columns) => (this.columns = columns)"
      :onRowSelectChange="selectChange"
    >
    </FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right',
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()">添加</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
  </div>
</template>
<script>
// import { getSelfList }  from '@/api/claim/dealer/ClaimApplication/index';
import { findRepairOrderClaim } from '@/api/dictionary';

export default {
  props: ['rowDate'],
  // mixins: [dictionary],
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchApi: findRepairOrderClaim,
      fetchParams: {
        vin: this.rowDate.vin,
      }, //ipa 接口
      selectedRows: [], //选中经销商
    };
  },
  created() {},
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '配件代码', fieldName: 'partNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '配件名称', fieldName: 'partName', placeholder: '请输入', initialValue: '' },
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        { title: '自费工单号', dataIndex: 'roNo', sorter: true },
        // { title: '自费工单号', dataIndex: 'selfNo', sorter: true },
        { title: '自费配件销售日期', dataIndex: 'roSalesDate', sorter: true, dateFormat: 'yyyy-MM-dd HH:mm:ss' },
        // { title: '自费配件销售日期', dataIndex: 'selfSdate', sorter: true },
        { title: '自费配件销售里程', dataIndex: 'roSalesMileage', sorter: true },
        // { title: '自费配件销售里程', dataIndex: 'selfMile', sorter: true },
        {
          title: '自费配件使用时长',
          dataIndex: 'selfUseHours',
          sorter: true,
          render: (props) => {
            return <span>{this.monDIff(this.rowDate.serviceBeginDate, props.row.roSalesDate)}</span>;
          },
        },
        {
          title: '自费配件使用里程',
          dataIndex: 'selfUseMile',
          sorter: true,
          render: (props) => {
            return <span>{this.calculateMileage(this.rowDate.driveMile, props.row.roSalesMileage)}</span>;
          },
        },
      ];
    },
    //选中
    selectChange(rows) {
      this.selectedRows = [...rows];
      this.selectedRows.map(x => {
        x.selfNo = x.roNo;
        x.selfSdate = x.roSalesDate;
        x.selfMile = x.roSalesMileage;
        x.selfUseHours = this.monDIff(this.rowDate.serviceBeginDate, x.roSalesDate);
        x.selfUseMile = this.calculateMileage(this.rowDate.driveMile, x.roSalesMileage);
      });
      console.log(this.selectedRows);
    },
    //保存
    saveDialog() {
      this.$emit('save', this.selectedRows);
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    chooseOrderNo() {
      this.visible = true;
    },
    monDIff(startTime, endTime) {
      startTime = new Date(startTime);
      endTime = new Date(endTime);
      var date2Mon;
      var startDate = startTime.getDate() + startTime.getHours() / 24 + startTime.getMinutes() / 24 / 60;
      var endDate = endTime.getDate() + endTime.getHours() / 24 + endTime.getMinutes() / 24 / 60;
      if (endDate >= startDate) {
        date2Mon = 0;
      } else {
        date2Mon = -1;
      }
      return (endTime.getYear() - startTime.getYear()) * 12 + endTime.getMonth() - startTime.getMonth() + date2Mon;
    },
    calculateMileage(driveMile, roSalesMileage) {
      driveMile = Number(driveMile) || 0;
      roSalesMileage = Number(roSalesMileage) || 0;
      return driveMile - roSalesMileage;
    },
    // myTime(date) {
    //   var arr = date.split('T');
    //   var d = arr[0];
    //   var darr = d.split('-');

    //   var t = arr[1];
    //   var tarr = t.split('.000');
    //   var marr = tarr[0].split(':');

    //   var dd = parseInt(darr[0]) + '/' + parseInt(darr[1]) + '/' + parseInt(darr[2]) + ' ' + parseInt(marr[0]) + ':' + parseInt(marr[1]) + ':' + parseInt(marr[2]);
    //   return dd;
    // },
  },
  // mounted() {
  //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
  // }
};
</script>

<style lang="less" scoped></style>
